package com.mzba.easy.weibo.widget;

import java.util.Map;
import java.util.Set;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.util.Linkify;
import android.util.AttributeSet;
import android.widget.TextView;
/**
 * 
 * @author 06peng
 *
 */
public class CustomTextView extends TextView {
	
	private static final int NAMELENGTH = 15; 
	private CharSequence text;
	private Map<String, String> faceMap;
	String font1 = "<font color=#419AD9>";
	String font2 = "</font>";
	
	private ImageGetter imageGetter = new Html.ImageGetter() {
        @Override
        public Drawable getDrawable(String source) {
            Drawable drawable = null;
			String sourceName = getContext().getPackageName() + ":drawable/" + source;
			int id = getResources().getIdentifier(sourceName, null, null);
			if (id != 0) {
				drawable = getResources().getDrawable(id);
				if (drawable != null) {
					drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
				}
			}
			return drawable;
        }
	};

	public CustomTextView(Context context) {
		super(context);
		setAutoLinkMask(Linkify.ALL);
	}
	
	public CustomTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setAutoLinkMask(Linkify.ALL);
	}

	public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setAutoLinkMask(Linkify.ALL);
	}
	
	public void setText(CharSequence text, String username, Map<String, String> faceMap){
		this.faceMap = faceMap;
		if (username != null) {
			String result = username.replace(username, font1 + username + font2);
			text = result + text;
		}
		setText(text);
	}

	@Override
	public CharSequence getText() {
		return text == null ? "" : text;
	}

	@Override
	public void setText(CharSequence text, BufferType type) {
		this.text = text;
		String cs = text.toString();
		
		//找以'@'开头以':'或' '结尾的子串，将其使用font标记进行修饰
		int start = 0;
		while (true) {
			start = cs.indexOf('@', start);
			if (start < cs.length() && start >= 0) {
				int end = cs.indexOf(' ', start);
				if (end < cs.length() && end > 0 && end - start <= NAMELENGTH) {
					CharSequence subcs = new String(cs.subSequence(start, end).toString());
					cs = cs.replace(subcs, font1 + subcs + font2);
					start += font1.length() + subcs.length() + font2.length();
				} else {
					end = cs.indexOf(':', start);
					if (end < cs.length() && end > 0 && end - start <= NAMELENGTH) {
						CharSequence subcs = new String(cs.subSequence(start, end).toString());
						cs = cs.replace(subcs, font1 + subcs + font2);
						start += font1.length() + subcs.length() + font2.length();
					}
				}
				if (cs.indexOf("@") != -1 && cs.indexOf(":") == -1) {
					CharSequence subcs = new String(cs.subSequence(cs.indexOf("@"), cs.length()).toString());
					cs = cs.replace(subcs, font1 + subcs + font2);
					start += font1.length() + subcs.length() + font2.length();
				}
				start += 1;
			} else {
				break;
			}
		}

		if (faceMap != null) {
			// 对表情符以img标记进行修饰，改用drawable显示出来
			Set<String> keys = faceMap.keySet();
			for (String key : keys) {
				if (cs.contains(key)) {
					cs = cs.replace(key, "<img src='" + faceMap.get(key) + "'>");
				}
			}
		}

		super.setText(Html.fromHtml(cs, imageGetter, null), type);
	}

}
